//master_concern
//关注达人并更新达人被关注数
exports.master_concern = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var masterConcernData = 0;

    var is_concern = 0;
    //任务流创建
    async.waterfall([
            //1.查询用户是否已关注当前问题
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchMasterConcern + "/mc_uid/" + bodyParams.mc_uid + "/mc_type/" + bodyParams.mc_type + "/mc_master_id/" + bodyParams.mc_master_id,
                        method: 'POST',
                        data: {
                            fields: ["mc_id", "is_delete"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        is_concern = data.data;
                        cb();
                    }
                );

            },

            //查询关注数
            function (cb) {
                if (bodyParams.mc_type == 1) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchOrganization + "/o_uid/" + bodyParams.mc_master_id,
                            method: 'POST',
                            data: {
                                fields: ["o_id", "o_concern_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            masterConcernData = data.data;
                            cb();
                        }
                    );
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_master + "/m_uid/" + bodyParams.mc_master_id,
                            method: 'POST',
                            data: {
                                fields: ["m_uid", "m_fans_count"].join(",")
                            }
                        },
                        function (err, data) {
                            //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            masterConcernData = data.data;
                            cb();
                        }
                    );
                }


            },
            function (cb) {
                //已关注
                if (masterConcernData.length > 0) {
                    if (is_concern.length > 0) {
                        if (is_concern[0].is_delete == 0) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateMasterConcern,
                                    method: 'POST',
                                    data: {
                                        mc_id: is_concern[0].mc_id,
                                        is_delete: 1
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    resultData.cancleZan = "已取消关注";
                                    cb();
                                }
                            );
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateMasterConcern,
                                    method: 'POST',
                                    data: {
                                        mc_id: is_concern[0].mc_id,
                                        is_delete: 0
                                    }
                                },
                                function (err, data) {
                                    // //console.log(data);
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }
                                    resultData.reTouchZan = "关注成功";
                                    cb();
                                }
                            );
                        }
                        //未关注
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.addMasterConcern,
                                method: 'POST',
                                data: {
                                    mc_uid: bodyParams.mc_uid,
                                    mc_master_id: bodyParams.mc_master_id,
                                    mc_type: bodyParams.mc_type
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.addData = "关注成功";
                                cb();
                            }
                        );
                    }
                } else {
                    cb();
                }

            },

            //3.关注数更新
            function (cb) {
                if (masterConcernData.length > 0) {
                    if (bodyParams.mc_type == 1) {
                        if (is_concern.length !== 0) {

                            if (is_concern[0].is_delete <= 0) {
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.updateOrganization,
                                        method: 'POST',
                                        data: {
                                            o_id: masterConcernData[0].o_id,
                                            o_concern_count: parseInt(masterConcernData[0].o_concern_count) - 1
                                        }
                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return RES.response(res, false, data);
                                        }

                                        resultData.updateData = data;
                                        cb();
                                    }
                                );
                            } else {
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.updateOrganization,
                                        method: 'POST',
                                        data: {
                                            o_id: masterConcernData[0].o_id,
                                            o_concern_count: parseInt(masterConcernData[0].o_concern_count) + 1
                                        }
                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return RES.response(res, false, data);
                                        }

                                        resultData.updateData = data;
                                        cb();
                                    }
                                );
                            }
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateOrganization,
                                    method: 'POST',
                                    data: {
                                        o_id: masterConcernData[0].o_id,
                                        o_concern_count: parseInt(masterConcernData[0].o_concern_count) + 1
                                    }
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }

                                    resultData.updateData = data;
                                    cb();
                                }
                            );
                        }
                    } else {
                        if (is_concern.length !== 0) {

                            if (is_concern[0].is_delete <= 0) {
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.updateMaster,
                                        method: 'POST',
                                        data: {
                                            m_uid: bodyParams.mc_master_id,
                                            m_fans_count: parseInt(masterConcernData[0].m_fans_count) - 1
                                        }
                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return RES.response(res, false, data);
                                        }

                                        resultData.updateData = data;
                                        cb();
                                    }
                                );
                            } else {
                                R.SEND_HTTP(
                                    req,
                                    {
                                        url: CORE_APIConfig.coreAPI.updateMaster,
                                        method: 'POST',
                                        data: {
                                            m_uid: bodyParams.mc_master_id,
                                            m_fans_count: parseInt(masterConcernData[0].m_fans_count) + 1
                                        }
                                    },
                                    function (err, data) {
                                        //判断是否有错误信息
                                        if (err) {
                                            return RES.response(res, false, data);
                                        }

                                        resultData.updateData = data;
                                        cb();
                                    }
                                );
                            }
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.updateMaster,
                                    method: 'POST',
                                    data: {
                                        m_uid: bodyParams.mc_master_id,
                                        m_fans_count: parseInt(masterConcernData[0].m_fans_count) + 1
                                    }
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, data);
                                    }

                                    resultData.updateData = data;
                                    cb();
                                }
                            );
                        }
                    }

                } else {
                    return RES.response(res, false, "该用户不是达人，无法关注");
                }


            }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};